package com.telnetnz.ticketdesk.dao.wrapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import com.rockbb.common.dao.CacheManagerBean;
import com.telnetnz.ticketdesk.dao.entity.OperationLogEntity;
import com.telnetnz.ticketdesk.dao.mapper.OperationLogMapper;

public class OperationLogWrap {

	private OperationLogMapper mapper;
	private Cache cache;

	public static final String[] ORDER_BY = {"log_time", "object_id"};
	public static final String[] ORDER = {"DESC", "ASC"};

	public OperationLogWrap(OperationLogMapper mapper,
			CacheManagerBean cacheManagerBean) {
		this.mapper = mapper;
		this.cache = cacheManagerBean.get(mapper.getClass().getName());
	}

	public OperationLogEntity select(long id) {
		return mapper.select(id);
	}

	public void insert(OperationLogEntity entity) {
		mapper.insert(entity);
	}

	public void delete(OperationLogEntity entity) {
		mapper.delete(entity);
	}

	public List<OperationLogEntity> selectByPageAndCondition(
			int start,
			int limit,
			int orderby,
			int order,
			long userId,
			int objectType,
			long objectId,
			String action,
			Date createAfter,
			Date createBefore) 
	{
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (start < 0) {parameters.put("start", 0);}
		else {parameters.put("start", start);}

		if (limit <=0) {parameters.put("limit", 20);}
		else {parameters.put("limit", limit);}

		if (orderby > ORDER_BY.length - 1 || orderby < 0) {parameters.put("orderby", ORDER_BY[0]);}
		else {parameters.put("orderby", ORDER_BY[orderby]);}

		parameters.put("order", (order == 0) ? ORDER[0] : ORDER[1]);

		if (userId > 0) {parameters.put("user_id", userId);}
		if (objectType > 0) {parameters.put("object_type", objectType);}
		if (objectId > 0) {parameters.put("object_id", objectId);}
		if (action != null && action.length() > 0) {parameters.put("action", action);}
		if (createAfter != null) parameters.put("create_after", createAfter);
		if (createBefore != null) parameters.put("create_before", createBefore);

		return mapper.selectByPageAndCondition(parameters);
	}

	public long countByCondition(
			long userId,
			int objectType,
			long objectId,
			String action,
			Date createAfter,
			Date createBefore) 
	{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (userId > 0) {parameters.put("user_id", userId);}
		if (objectType > 0) {parameters.put("object_type", objectType);}
		if (objectId > 0) {parameters.put("object_id", objectId);}
		if (action != null && action.length() > 0) {parameters.put("action", action);}
		if (createAfter != null) parameters.put("create_after", createAfter);
		if (createBefore != null) parameters.put("create_before", createBefore);

		return mapper.countByCondition(parameters);
	}

	public long countAll() {
		return mapper.countAll();
	}

	public Cache getCache() {
		return cache;
	}
}
